package com.sgb.kjwl.viewmodel.house;

import androidx.databinding.ObservableField;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseListEntity;
import com.sgb.kjwl.model.entity.request.HouseFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.house.LeaseHouseListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseHouseListFragModel extends BaseViewModel {
    public ObservableField<HouseFilterJson> filterJson = new ObservableField<>(new HouseFilterJson());
    //    public ObservableField<String> areaCode = new ObservableField<>("");
    private LeaseHouseListFragment fragment;
    private MachineRepertory repertory;
    private int page = 1;
    public int pageSize = 10;

    public LeaseHouseListFragModel(LeaseHouseListFragment fragment, MachineRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
//        areaCode.set(repertory.getAreaCode());
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    //获取顶部分类
    public void getFilterTypeModel(String type) {
        APIManager.getInstance().getLeaseMapAPI().getFistcategory(type).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    fragment.showTypePopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }

//    //获取一级类型
//    public void getFilterTypeModel() {
//        APIManager.getInstance().getLeaseAPI().getParentEstateTypeHouse().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
//                if (response.body() == null || response.body().getState() == null) return;
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    fragment.showTypePopWindow(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
//            }
//        });
//
//    }

    //获取房产二级类型
    public void getFilterModelModel(String id) {
        APIManager.getInstance().getLeaseAPI().getChildTypeHouse(id).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showBrandPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });
    }

    //获取面积类型列表
    public void getAreaNumModel() {
        APIManager.getInstance().getLeaseAPI().getAreaNumHouse().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showRentalAreaPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }

    //    //获取区域列表
//    public void getMachineAreaModel() {
//        repertory.getAreaSecondAndThird(repertory.getAreaCode())
//                .subscribe(new Consumer<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>> listBaseEntity) throws Exception {
//                        if (listBaseEntity == null || listBaseEntity.getState() == null) return;
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showAreaPop(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                    }
//                });
//    }
//
//    public void getCityData() {
//        APIManager.getInstance().getMachineAPI().getAreaSecondAndThird2(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    fragment.showAreaPop(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
//
//            }
//        });
//    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    fragment.showAddressDatas(data, repertory.getAreaCode());
                }
            }
        });
    }

    //获取房产排序列表
    public void getFilterSort() {
        APIManager.getInstance().getLeaseAPI().getLeaseHomeSortWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showSortPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }


    public void getAllList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        filterJson.get().setPageNum(page);
        filterJson.get().setPageSize(pageSize);
        filterJson.get().setType("4");
        if (repertory.getIsLocal()) {
            //经度
            filterJson.get().setLatitude("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""))
                    ? repertory.getLatitude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""));
            //纬度
            filterJson.get().setLongitude("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, ""))
                    ? repertory.getLongititude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, ""));
        } else {
            //经度
            filterJson.get().setLongitude(repertory.getLongititude());
            //纬度
            filterJson.get().setLatitude(repertory.getLatitude());
        }
        //获取租赁手动定位缓存
        String distance_latitude = SharedPreferenceUtil.getInstance().getSaveStringData("distance_latitude", "");
        String distance_longitude = SharedPreferenceUtil.getInstance().getSaveStringData("distance_longitude", "");
        if (!TextUtils.isEmpty(distance_latitude) && !TextUtils.isEmpty(distance_longitude)) {
            filterJson.get().setLatitude(distance_latitude);
            filterJson.get().setLongitude(distance_longitude);
        }
        APIManager.getInstance().getLeaseAPI().rentalAndEmployedHall(filterJson.get()).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseListEntity>>>>(true) {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LeaseListEntity>>> data) {
                if (data.getData() != null) {
                    fragment.showList(data.getData().getList(), isLoadMore);
                } else {
                    fragment.showList(new ArrayList<>(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LeaseListEntity>>>> call, Throwable t) {
                fragment.showList(new ArrayList<>(), isLoadMore);
            }
        });
    }

//    //获取房产筛选列表
//    public void getAllList(boolean isLoadMore) {
//        page = isLoadMore ? page + 1 : 1;
//        filterJson.get().setPageNum(page);
//        filterJson.get().setPageSize(pageSize);
//        if (TextUtils.isEmpty(filterJson.get().getSortId())) {
//            filterJson.get().setSortId("1");
//        }
//        if (filterJson.get().getCategoryParentId() == null) {
//            filterJson.get().setCategoryParentId("");
//        }
//        if (filterJson.get().getCategoryChildId() == null) {
//            filterJson.get().setCategoryChildId("");
//        }
//        if (TextUtils.isEmpty(filterJson.get().getRentalArea())) {
//            filterJson.get().setRentalArea("");
//        }
//        DialogHelper.showProgressDialog(fragment.getContext(), "加载中...", 0, false).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getLeaseAPI().getAllListHouse(filterJson.get())
//                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseHousesEntity>>>>() {
//                    @Override
//                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseHousesEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseHousesEntity>>>> response) {
//                        DialogHelper.dismissProgressDialog();
//                        if (response.body() == null || response.body().getState() == null) {
//                            return;
//                        }
//                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                            fragment.showList(response.body().getData().getList(), isLoadMore);
//                        }
//
//                    }
//
//                    @Override
//                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseHousesEntity>>>> call, Throwable t) {
//                        DialogHelper.dismissProgressDialog();
//                    }
//                });
//    }


    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("propertyDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&iosFlag=1";
//                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                    WebViewX5HostActivity.start(fragment.getActivity(), url, id, "lease", "4");
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
